/**
 * 验证码广告基本函数库文件
 * 
 * 本函数库文件仅用于合作网站使用本公司验证码使用
 * @author ××科技网络有限公司
 * @copyright 2011-06-20
 */

(function(window){
	if(window.CAPture){
		return;
	}
	window.CAPture = {};
	var CAPture = window.CAPture;
	//基本配置
	CAPture.conf = {
		onSuccess : null,
		onError : null,
		container : null,
		textObj : null,
		siteID : '',
		refreshTextClass : '',	//刷新文字式样class
		linkClass : '',	//链接部分式样class
	};
	/**
	 * 初始化验证码
	 */
	CAPture.init = function(o){
		for(var i in o){
			CAPture.conf[i] = o[i];
		}
		CAPture.conf.textObj.onchange = function(){
			CAPture.check(this.value);
		};
		CAPture.refresh();
	};
	/**
	 * 刷新验证码
	 */
	CAPture.refresh = function(){
		var s = document.createElement('script');
		s.acceptCharset = 'utf-8';
		s.src = 'http://cap.mk/refresh/'+CAPture.conf.siteID+'?'+Math.ceil(Math.random()*1000);
		window.document.body.appendChild(s);
	};
	/**
	 * 检查验证码
	 */
	CAPture.check = function(v){
		var s = document.createElement('script');
		s.acceptCharset = 'utf-8';
		s.src = 'http://cap.mk/check/'+CAPture.conf.siteID+'?v='+encodeURIComponent(v)+'&'+Math.ceil(Math.random()*1000);
		window.document.body.appendChild(s);
	};
	/**
	 * 设置COOKIE
	 */
	CAPture.setCookie = function(n, v){
		window.document.cookie = n + escape(v);
	};
	/**
	 * 验证成功后的执行函数
	 */
	CAPture.onSuccess = function(){
		if(typeof CAPture.conf.onSuccess == 'function'){
			CAPture.conf.onSuccess();
		}
	};
	/**
	 * 验证失败的执行函数
	 */
	CAPture.onError = function(){
		if(typeof CAPture.conf.onError == 'function'){
			CAPture.conf.onError();
		}
	};
	CAPture.loadImg = function(o){
		var c = CAPture.conf.container;
		//生成图片
		var i = document.createElement('img');
		i.src = 'http://cap.mk/img/'+CAPture.conf.siteID+'?'+Math.ceil(Math.random()*1000);
		c.innerHTML = '';
		c.appendChild(i);
		//生成刷新文字
		var i = document.createElement('a');
		i.href = 'javascript:CAPture.refresh();';
		i.innerHTML = '换一张图片';
		i.className = CAPture.conf.refreshTextClass;
		c.appendChild(i);
		//生成链接文字
		c.appendChild(document.createElement('br'));
		
		var i = document.createElement('span');
		i.className = CAPture.conf.linkClass;
		i.innerHTML = o.html;
		c.appendChild(i);
	};
})(window);